import Vue from 'vue'
import Vuex from 'vuex'
const debug = process.env.NODE_ENV !== 'production'
Vue.use(Vuex)

const user = {
  state: {
    userData: 'userData'
  },
  getters: {
    getUser: (state) => {
      return state.userData
    }
  }
}

export default new Vuex.Store({
  strict: debug, // 在不是生产环境下都开启严格模式
  modules: {
    user
  },
  state: {
    myData: 'vuex data',
    arr: [1, 2, 3, 4, 5, 6]
  },
  getters: {
    getMyData: (state) => {
      return state.myData
    },
    filterArr: (state, getters) => {
      console.log(getters.getMyData)
      return state.arr.map(i => (i += 'a'))
    }
  },
  mutations: {
    setMyData(state, str) {
      state.myData = str
    }
  },
  actions: {
    asyncSetMyData({ state, commit }) {
      console.log(state)
      console.log(this)
      commit('setMyData', 'new data')
    },
    async asyncSet({ dispatch, commit }) {
      await dispatch('asyncSetMyData') // 等待asyncSetMyData 完成
      commit('setMyData', 'data')
    }
  }
})
